package interview;


class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
public class lc82 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next.next = new ListNode(5);
        head = deleteDuplicates(head);
        while(head != null){
            System.out.print(head.val + " ");
            head = head.next;
        }
    }
    //重复全部的删除
    public static ListNode deleteDuplicates2(ListNode head) {
        if(head == null || head.next == null)
            return head;
        //定义虚拟头节点
        ListNode dumpy = new ListNode();
        dumpy.next = head;
        //定义前驱节点
        ListNode pre = dumpy;
        //当前节点
        ListNode cur = head;
        while(cur != null && cur.next != null){
            if(cur.val == cur.next.val){
                //找到重复的进行循环
                int x = cur.val;
                while(cur != null && cur.val == x){
                    cur = cur.next;
                }
                pre.next = cur;
            }
            else{
                pre.next = cur;
                cur = cur.next;
                pre = pre.next;
            }
        }
        return dumpy.next;
    }

    //重复的保留一个
    public static ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)
            return head;
        //定义虚拟头节点
        ListNode dumpy = new ListNode();
        dumpy.next = head;
        //定义前驱节点
        ListNode pre = dumpy;
        //当前节点
        ListNode cur = head;
        while(cur != null && cur.next != null){
            if(cur.val == cur.next.val){
                int x = cur.val;
                while(cur.next != null && cur.next.val == x){
                    cur = cur.next;
                }
            }
            pre.next = cur;
            pre = pre.next;
            cur = cur.next;
        }
        return dumpy.next;
    }
}
